<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>copy</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Algorithms</a> &gt; <a href="copy.html">copy</a>
  </div>

  <div class="name-format">
    copy
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;algorithm&gt;
  iterator copy( iterator start, iterator end, iterator dest );
</pre>

  <p>The copy() function copies the elements between <em>start</em> and
  <em>end</em> to <em>dest</em>. In other words, after copy() has
  run,</p>
  <pre class="example-code">
 *dest == *start
 *(dest+1) == *(start+1)
 *(dest+2) == *(start+2)
 ...
 *(dest+N) == *(start+N)                
</pre>

  <p>The return value is an iterator to the last element copied. copy()
  runs in <a href="../complexity.html">linear time</a>.</p>

  <p>For example, the following code uses copy() to copy the contents
  of one vector to another:</p>
  <pre class="example-code">
 vector&lt;int&gt; from_vector;
 for( int i = 0; i &lt; 10; i++ ) {
   from_vector.push_back( i );
 }              

 vector&lt;int&gt; to_vector(10);               

 copy( from_vector.begin(), from_vector.end(), to_vector.begin() );             

 cout &lt;&lt; &quot;to_vector contains: &quot;;
 for( unsigned int i = 0; i &lt; to_vector.size(); i++ ) {
   cout &lt;&lt; to_vector[i] &lt;&lt; &quot; &quot;;
 }
 cout &lt;&lt; endl;            
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="copy_backward.html">copy_backward</a><br>
    <a href="copy_n.html">copy_n</a><br>
    <a href="generate.html">generate</a><br>
    <a href="remove_copy.html">remove_copy</a><br>
    <a href="swap.html">swap</a><br>
    <a href="transform.html">transform</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
